 
<?PHP
class Db_Mysql_Adapter extends Db_Adapter
{
    protected $lastError;
 
    public static function getInstance()
    {
        if(!parent::$rInstance)
        {
            parent::$rInstance    = new Db_Mysql_Adapter();
        }
        return parent::$rInstance;
    }
 
    function __construct()
    {
        if(!$this->_connect())
        {
            return false;
        }
    }
 
    function select($rgFrom,
            $rgCols=array('*'),
            $rgWhere=null,
            $rgGroup=null,
            $rgOrder=null,
            $rgHaving=null,
            $rgLimit=null)
    {
        
        if(!parent::$rLink)
        {
            return false;
        }
        $sql=$this->_get_select_sql($rgFrom, $rgCols, $rgWhere, $rgGroup, $rgOrder, $rgHaving, $rgLimit);
        if(!$sql)
        {
            return false;
        }
        $rgResul   = mysql_query($sql);
	 while($row   = mysql_fetch_array($rgResul,MYSQL_ASSOC)){
            $rgResult[]=$row;
        }
        if(!isset($rgResult))
        {
            $rgResult=false;
        }
               return $rgResult;
    }
 
    public function replace()
    {
 
    }
 
    public function update($table, $rgFields, $rgWhere=null)
    {
        if(!is_array($rgFields)||!count($rgFields))
        {
            return false;
        }
        $sql=$this->_get_update_sql($table, $rgFields, $rgWhere);
        $bResult    = $this->query($sql);
        return $bResult;
    }
 
    public function insert($table, $rgFields, $sPrimaryField='id')
    {
       /* if(!array_key_exists($sPrimaryField, $rgFields))
        {
            $rgFields[$sPrimaryField]=$this->_get_next_id($table, $sPrimaryField);
        }
*/       
        $sql=$this->_get_insert_sql($table, $rgFields);
        $bResult    = $this->query($sql);
        return $bResult;
    }
 
    public function delete($table, $rgWhere)
    {
        $sql=$this->_get_delete_sql($table, $rgWhere);
        $bResult    = $this->query($sql);
        return $bResult;
    }
 
    public function query($sql)
    {   echo $sql;
        return mysql_query($sql);
    }
 
    protected function _get_next_id($table, $sPrimaryField='id')
    {  
        $rgMax=$this->select($table, array('MAX('.$sPrimaryField.')'));
        echo $rgMax[0][0]+1;
        return $rgMax[0][0]+1;
    }
 
    protected function _connect()
    {
        $rDb            = new Db_Controller();
        $rgCredentials  = $rDb->getCredentials();
        if(!parent::$rLink)
        {
            try
            {
                parent::$rLink= mysql_connect($rgCredentials['database'],$rgCredentials['user'], $rgCredentials['password']);
                mysql_select_db("lifetogo");
                if(!parent::$rLink)
                {
                    echo "не подключилась";
                    return false;
                }
            }
            catch(Exception $e)
            {
                return false;
            }
        }
        return true;
    }
 
    protected function  _escape_string($string)
    {
        return mysql_escape_string($string);
    }
 
}
?>
 